# xmlscenereader.py
#
# Read the XML that describes a scene

import xmlscene
from xml.dom import minidom
from xml.dom import Node

import unittest

def readScene(scene_file):
    doc = minidom.parse(scene_file)
    rootNode = doc.documentElement
    scene = xmlscene.scene.factory()
    scene.build(rootNode)
    # Enable Python to collect the space used by the DOM.
    doc = None
    return scene

if __name__ == '__main__':
    class TestReadScene(unittest.TestCase):

        def setUp(self):
            self.file = "./xml_test_files/test_scene_xmlscenereader.xml"
            self.scene = readScene(self.file)

        def test_scene_not_none(self):
            # make sure the scene is not none
            self.assert_(self.scene is not None)
            self.assert_(isinstance(self.scene, xmlscene.scene)) 

        def test_scene_has_head_and_body(self):
            # make sure the scene has a head and body
            head = self.scene.get_head()
            self.assert_(head is not None)
            self.assert_(isinstance(head, xmlscene.head))
            
            body = self.scene.get_body()
            self.assert_(body is not None)
            self.assert_(isinstance(body, xmlscene.body))

        def test_head_section(self):
            # test the head section
            head = self.scene.get_head()
            
            objectdefs = head.get_objectdef()
            self.assert_(objectdefs is not None)
            self.assert_(len(objectdefs) == 5)
            
            xyplane = objectdefs[0]
            self.assert_(isinstance(xyplane, xmlscene.objectdef))
            self.assert_(xyplane.get_name() == "xyplane")
            triangleset = xyplane.get_triangleset()[0]
            self.assert_(isinstance(triangleset, xmlscene.triangleset))
            self.assert_(triangleset.get_name() == "first")
            self.assert_(triangleset.get_reflectivity() == ".9,.9,.9")
            self.assert_(triangleset.get_radiosity() == "0,0,0")
            self.assert_(triangleset.get_emission() == "0,0,0")
            triangles = triangleset.get_triangle()
            self.assert_(triangles is not None)
            self.assert_(len(triangles) == 1)
            vertices = triangles[0].get_vertex()
            self.assert_(len(vertices) == 3)
            vertex0 = vertices[0]
            self.assert_(isinstance(vertex0, xmlscene.vertex))
            self.assert_(vertex0.get_x() == 10.0)
            self.assert_(vertex0.get_y() == -10.0)
            self.assert_(vertex0.get_z() == 0.0)
            vertex1 = vertices[1]
            self.assert_(vertex1.get_x() == 10.0)
            self.assert_(vertex1.get_y() == 10.0)
            self.assert_(vertex1.get_z() == 0.0)
            vertex2 = vertices[2]
            self.assert_(vertex2.get_x() == -10.0)
            self.assert_(vertex2.get_y() == -10.0)
            self.assert_(vertex2.get_z() == 0.0)
            triangle_nexts = triangleset.get_trianglenext()
            self.assert_(len(triangle_nexts) == 1)
            self.assert_(isinstance(triangle_nexts[0], xmlscene.trianglenext))
            vertex = triangle_nexts[0].get_vertex()
            self.assert_(vertex.get_x() == -10.0)
            self.assert_(vertex.get_y() == 10.0)
            self.assert_(vertex.get_z() == 0.0)
            
            xyplane_green = objectdefs[1]
            self.assert_(xyplane_green.get_name() == "xyplane-green")
            
            black_barrel = objectdefs[2]
            self.assert_(black_barrel.get_name() == "black-barrel")
            barrel = black_barrel.get_barrel()[0]
            self.assert_(isinstance(barrel, xmlscene.barrel))
            self.assert_(barrel.get_reflectivity() == ".1,.1,.1")
            self.assert_(barrel.get_radiosity() == "0,0,0")
            self.assert_(barrel.get_emission() == "0,0,0")
            
            light_source = objectdefs[3]
            self.assert_(light_source.get_name() == "light-source")
            cuboid = light_source.get_cuboid()[0]
            self.assert_(isinstance(cuboid, xmlscene.cuboid))
            self.assert_(cuboid.get_emission() == "1,1,1")
            self.assert_(cuboid.get_reflectivity() == "0,0,0")
            self.assert_(cuboid.get_radiosity() == "0,0,0")
            
            teapot_obj = objectdefs[4]
            self.assert_(teapot_obj.get_name() == "teapot")
            teapot = teapot_obj.get_teapot()[0]
            self.assert_(isinstance(teapot, xmlscene.teapot))
            self.assert_(teapot.get_reflectivity() == ".8,.8,0")
            self.assert_(teapot.get_radiosity() == "0,0,0")
            self.assert_(teapot.get_emission() == "0,0,0")
            
        def test_body_section(self):
            # test the body section
            body = self.scene.get_body()
            
            apply_macro = body.get_apply_macro()
            self.assert_(apply_macro.get_name() == "viewScene")
            
            macros = body.get_macro()
            self.assert_(len(macros) == 4)
            
            macro_scene = macros[0]
            self.assert_(isinstance(macro_scene, xmlscene.macro))
            self.assert_(macro_scene.get_name() == "scene")
            apply_macros = macro_scene.get_apply_macro()
            self.assert_(len(apply_macros) == 1)
            apply_macro = apply_macros[0]
            self.assert_(isinstance(apply_macro, xmlscene.apply_macro))
            self.assert_(apply_macro.get_name() == "room")
            translates = macro_scene.get_translate()
            self.assert_(len(translates) == 3)
            translate = translates[0]
            self.assert_(isinstance(translate, xmlscene.translate))
            self.assert_(translate.get_dy() == 6)
            self.assert_(translate.get_dz() == 4)
            self.assert_(translate.get_dx() == 0)
            apply_macros = translate.get_apply_macro()
            self.assert_(len(apply_macros) == 1)
            apply_macro = apply_macros[0]
            self.assert_(apply_macro.get_name() == "light")
            translate = translates[1]
            self.assert_(translate.get_dy() == 6)
            self.assert_(translate.get_dz() == -4)
            self.assert_(translate.get_dx() == 0)
            apply_macros = translate.get_apply_macro()
            self.assert_(len(apply_macros) == 1)
            apply_macro = apply_macros[0]
            self.assert_(apply_macro.get_name() == "light")
            translate = translates[2]
            self.assert_(translate.get_dy() == -8)
            self.assert_(translate.get_dz() == 5)
            self.assert_(translate.get_dx() == 0)
            rotates = translate.get_rotate()
            self.assert_(len(rotates) == 1)
            rotate = rotates[0]
            self.assert_(rotate.get_angle() == 45)
            self.assert_(rotate.get_dy() == -1)
            scales = rotate.get_scale()
            self.assert_(len(scales) == 1)
            scale = scales[0]
            self.assert_(scale.get_dx() == 2)
            self.assert_(scale.get_dy() == 2)
            self.assert_(scale.get_dz() == 2)
            objects = scale.get_object()
            self.assert_(len(objects) == 1)
            object = objects[0]
            self.assert_(object.get_name() == "teapot")
            
            macro_scene = macros[1]
            self.assert_(macro_scene.get_name() == "viewScene")
            translates = macro_scene.get_translate()
            self.assert_(len(translates) == 1)
            translate = translates[0]
            self.assert_(translate.get_dx() == -2)
            rotates = translate.get_rotate()
            self.assert_(len(rotates) == 1)
            rotate = rotates[0]
            self.assert_(rotate.get_angle() == 60)
            self.assert_(rotate.get_dy() == 1)
            self.assert_(rotate.get_dx() == 0)
            self.assert_(rotate.get_dz() == 0)
            scales = rotate.get_scale()
            self.assert_(len(scales) == 1)
            scale = scales[0]
            self.assert_(scale.get_dx() == 1.5)
            self.assert_(scale.get_dy() == 1.5)
            self.assert_(scale.get_dz() == 1.8)
            apply_macros = scale.get_apply_macro()
            self.assert_(len(apply_macros) == 1)
            apply_macro = apply_macros[0]
            self.assert_(apply_macro.get_name() == "scene")
            
            macro_scene = macros[2]
            self.assert_(macro_scene.get_name() == "room")
            translates = macro_scene.get_translate()
            self.assert_(len(translates) == 6)
            translate = translates[0]
            self.assert_(translate.get_dx() == 10)
            self.assert_(translate.get_dy() == 0)
            self.assert_(translate.get_dz() == 0)
            rotates = translate.get_rotate()
            self.assert_(len(rotates) == 1)
            rotate = rotates[0]
            self.assert_(rotate.get_angle() == 90)
            self.assert_(rotate.get_dx() == 0)
            self.assert_(rotate.get_dy() == 1)
            self.assert_(rotate.get_dz() == 0)
            objects = rotate.get_object()
            self.assert_(len(objects) == 1)
            object = objects[0]
            self.assert_(object.get_name() == "xyplane")
            translate = translates[1]
            self.assert_(translate.get_dx() == -10)
            self.assert_(translate.get_dy() == 0)
            self.assert_(translate.get_dz() == 0)
            rotates = translate.get_rotate()
            self.assert_(len(rotates) == 1)
            rotate = rotates[0]
            self.assert_(rotate.get_angle() == 90)
            self.assert_(rotate.get_dx() == 0)
            self.assert_(rotate.get_dy() == -1)
            self.assert_(rotate.get_dz() == 0)
            objects = rotate.get_object()
            self.assert_(len(objects) == 1)
            object = objects[0]
            self.assert_(object.get_name() == "xyplane")
            translate = translates[2]
            self.assert_(translate.get_dz() == -10)
            self.assert_(translate.get_dx() == 0)
            self.assert_(translate.get_dy() == 0)
            objects = translate.get_object()
            self.assert_(len(objects) == 1)
            object = objects[0]
            self.assert_(object.get_name() == "xyplane")
            translate = translates[3]
            self.assert_(translate.get_dz() == 10)
            rotates = translate.get_rotate()
            rotate = rotates[0]
            self.assert_(rotate.get_angle() == 180)
            self.assert_(rotate.get_dx() == 0)
            self.assert_(rotate.get_dy() == 1)
            self.assert_(rotate.get_dz() == 0)
            object = rotate.get_object()[0]
            self.assert_(object.get_name() == "xyplane")
            translate = translates[4]
            self.assert_(translate.get_dy() == 10)
            rotate = translate.get_rotate()[0]
            self.assert_(rotate.get_angle() == 90)
            self.assert_(rotate.get_dx() == 1)
            object = rotate.get_object()[0]
            self.assert_(object.get_name() == "xyplane")
            translate = translates[5]
            self.assert_(translate.get_dy() == -10)
            rotate = translate.get_rotate()[0]
            self.assert_(rotate.get_angle() == 90)
            self.assert_(rotate.get_dx() == -1)
            object = rotate.get_object()[0]
            self.assert_(object.get_name() == "xyplane-green")
            
            macro = macros[3]
            self.assert_(macro.get_name() == "light")
            translates = macro.get_translate()
            self.assert_(len(translates) == 3)
            translate = translates[0]
            self.assert_(translate.get_dx() == -1.5)
            self.assert_(translate.get_dy() == 2)
            scale = translate.get_scale()[0]
            self.assert_(scale.get_dx() == 1)
            self.assert_(scale.get_dz() == 1)
            self.assert_(scale.get_dy() == 4)
            object = scale.get_object()[0]
            self.assert_(object.get_name() == "black-barrel")
            translate = translates[1]
            self.assert_(translate.get_dx() == 1.5)
            self.assert_(translate.get_dy() == 2)
            scale = translate.get_scale()[0]
            self.assert_(scale.get_dx() == 1)
            self.assert_(scale.get_dz() == 1)
            self.assert_(scale.get_dy() == 4)
            object = scale.get_object()[0]
            self.assert_(object.get_name() == "black-barrel")
            translate = translates[2]
            self.assert_(translate.get_dy() == -1)
            scale = translate.get_scale()[0]
            self.assert_(scale.get_dx() == 6)
            self.assert_(scale.get_dz() == 4)
            self.assert_(scale.get_dy() == 2)
            object = scale.get_object()[0]
            self.assert_(object.get_name() == "light-source")
            
    unittest.main()
